package com.sunshine.lnuplus.view

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.sunshine.lnuplus.R
import com.sunshine.lnuplus.ui.activity.WebViewActivity
import es.dmoral.toasty.Toasty

class DialogWithoutView : DialogFragment() {

    companion object{
        private var positive: DialogInterface.OnClickListener? = null
        private var negative: DialogInterface.OnClickListener? = null

        @JvmStatic
        fun newInstance(
            title: String,
            message: String,
            cancelable: Boolean,
            positive: DialogInterface.OnClickListener?,
            negative: DialogInterface.OnClickListener?
        ) : DialogWithoutView{
            this.positive = positive
            this.negative = negative
            val bundle = Bundle()
            bundle.apply {
                putString("title", title)
                putString("message", message)
                putBoolean("cancelable",cancelable)
            }
            val dialogWithoutView = DialogWithoutView()
            dialogWithoutView.arguments = bundle
            return dialogWithoutView
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = MaterialAlertDialogBuilder(context).apply {
            setTitle(arguments?.getString("title"))
            setMessage(arguments?.getString("message"))
            when {
                arguments?.getString("title").equals("哎呀，课程表好像出了些问题") -> {
                    setPositiveButton("去反馈", positive)
                    setNegativeButton("清除数据库继续", negative)
                    setNeutralButton("取消", null)
                }
                arguments?.getString("title").equals("Shine课程表有新版本啦") -> {
                    setPositiveButton("更新", positive)
                    setNegativeButton("浏览器下载", negative)
                    setNeutralButton("取消", null)
                }
                arguments?.getString("title").equals("感谢你") -> {
                    val positive = DialogInterface.OnClickListener { _, _ ->
                        try {
                            Toasty.info(context, "感谢！").show()
                            startActivity(Intent.parseUri(resources.getString(R.string.alipay_url), Intent.URI_INTENT_SCHEME))
                        }catch (e: java.lang.Exception){
                            Toasty.warning(context, "打开支付宝失败").show()
                        }
                    }
                    val negative = DialogInterface.OnClickListener { _, _ ->
                        try {
                            val str = "market://details?id=com.sunshine.lnuplus"
                            val localIntent = Intent(Intent.ACTION_VIEW, Uri.parse(str))
                            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            localIntent.`package` = "com.coolapk.market"
                            context.startActivity(localIntent)
                        } catch (e: Exception) {
                            try {
                                val str = "market://details?id=com.sunshine.lnuplus"
                                val localIntent = Intent(Intent.ACTION_VIEW, Uri.parse(str))
                                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                context.startActivity(localIntent)
                            }catch (e : Exception){
                                try {
                                    context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://www.coolapk.com/apk/com.sunshine.lnuplus")))
                                }catch (e: Exception){
                                    Toasty.error(context, "打开浏览器失败").show()
                                }
                            }
                        }
                    }
                    val neutral = DialogInterface.OnClickListener { _, _ ->
                        val bundle = Bundle()
                        bundle.putString("title","捐赠名单")
                        bundle.putString("url","https://support.qq.com/products/131050/blog/11091")
                        startActivity(Intent(activity, WebViewActivity::class.java).putExtras(bundle))
                    }
                    setPositiveButton("捐赠", positive)
                    setNegativeButton("去评价", negative)
                    setNeutralButton("捐赠名单", neutral)
                }
                arguments?.getString("title").equals("登录提醒") -> {
                    setPositiveButton("导入班级课程", positive)
                    setNegativeButton("继续登录", negative)
                }
                else -> {
                    setPositiveButton("确定", positive)
                    setNegativeButton("取消", negative)
                }
            }

        }
        return builder.create().apply {
            this@DialogWithoutView.isCancelable = arguments?.getBoolean("cancelable")!!
            if(context.getSharedPreferences("com.sunshine.lnuplus_preferences", Context.MODE_PRIVATE).getBoolean("night", false)){
                window?.setBackgroundDrawableResource(R.drawable.dialog_corner)
            }else{
                window?.setBackgroundDrawableResource(R.drawable.dialog_corner)
            }

        }
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(
            (activity!!.windowManager.defaultDisplay.width * 0.8).toInt(),
            WindowManager.LayoutParams.WRAP_CONTENT
        )
    }
}